package com.fangdada.demowebfulx2.service.impl;


import com.fangdada.demowebfulx2.entity.User;
import com.fangdada.demowebfulx2.service.UserService;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: fd
 * @Date 2021/11/30 10:04
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {

	//创建map集合存储数据
	private final Map<Integer, User> users = new HashMap<>();

	public UserServiceImpl() {
		users.put(1,new User("lucy","nan",20));
		users.put(2,new User("mary","nv",30));
		users.put(3,new User("jack","nv",50));
	}

	//根据id查询
	@Override
	public Mono<User> getUserById(int id) {
		return Mono.justOrEmpty(users.get(id));
	}

	//查询多个用户
	@Override
	public Flux<User> getAllUser() {
		return Flux.fromIterable(users.values());
	}

	//添加用户 Mono.empty() 终止信号
	@Override
	public Mono<Void> saveUserInfo(Mono<User> userMono) {
		return userMono.doOnNext(person -> {
			//向map集合里面放值
			int id = users.size()+1;
			users.put(id,person);
		}).thenEmpty(Mono.empty());
	}
}
